﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace iS3.DAL
{
    public class DGEntityManager
    {
        private static DGEntityManager instance = null;
        public static DGEntityManager GetInstance()
        {
            if (instance == null)
            {
                instance = new DGEntityManager();
            }
            return instance;
        }
        private Dictionary<string, DGEntityStruct> DGEntityStructDict;
        private Dictionary<string, List<PropertyInfo>> PropertyInfoDict;

        private DGEntityManager()
        {
            DGEntityStructDict = new Dictionary<string, DGEntityStruct>();
            PropertyInfoDict = new Dictionary<string, List<PropertyInfo>>();
        }
        public DGEntityStruct GetStruct(Type typeInfo)
        {
            if (!DGEntityStructDict.ContainsKey(typeInfo.Name))
            {
                //DGEntityStructDict[typeInfo.Name] = DGEntityStruct.LoadDesc(typeInfo);
                DGEntityStructDict[typeInfo.Name] = (Activator.CreateInstance(typeInfo) as DGBase).LoadDesc();
            }
            return DGEntityStructDict[typeInfo.Name];
        }
        public List<PropertyInfo> GetPropertys(Type typeInfo)
        {
            if (!PropertyInfoDict.ContainsKey(typeInfo.Name))
            {
                PropertyInfoDict[typeInfo.Name] = iS3ReflectTool.GetPropertyList(typeInfo);
            }
            return PropertyInfoDict[typeInfo.Name];
        }
        public PropertyInfo GetProperty(Type typeInfo,string propertyName)
        {
            if (!PropertyInfoDict.ContainsKey(typeInfo.Name))
            {
                PropertyInfoDict[typeInfo.Name] = iS3ReflectTool.GetPropertyList(typeInfo);
            }
            return PropertyInfoDict[typeInfo.Name].Where(x=>x.Name==propertyName).FirstOrDefault();
        }
    }
}
